//
//  BIDOwnerPersonalRepairDetailViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/7.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDOwnerPersonalRepairDetailViewController.h"
#import "BIDEvaluateForPersonalRepairViewController.h"
#import "BIDPersonalRepairStatusView.h"
#import "BIDSubPersonalRepairDetailView.h"
#import "BIDBrowserImageViewController.h"
#import "BIDDispatchInfoView.h"
#import "BIDEvaluateInfoView.h"

//报修详情
static NSString *repairDetailURL = @"client/repair/publicRepairInfo.action";
//关闭报修
static NSString *closeRepairURL = @"client/repair/closeRepair.action";
//确认完成或评价
static NSString *confirmRepairOrEvaluateURL = @"client/repair/confirmOrJudge.action";
//获取派工信息
static NSString *fetchDispatchInfoURL = @"client/repair/sendMenInfo.action";

@interface BIDOwnerPersonalRepairDetailViewController ()<BIDSubPersonalRepairDetailViewDelegate, BIDDispatchInfoViewDelegate, BIDEvaluateInfoViewDelegate>
{
    BIDPersonalRepairStatusView *_statusView;
    BIDSubPersonalRepairDetailView *_detailView;
    BOOL _isShowRepairStatus;
    NSInteger _curRepairStatus;
    UIView *_maskView;
    //评价内容和得分
    NSString *_evaluateContent;
    NSUInteger _evaluateScore;
}
@end

@implementation BIDOwnerPersonalRepairDetailViewController
@synthesize repairID;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"个人报修";
    _isShowRepairStatus = YES;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    _maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height-64)];
    [_maskView setBackgroundColor:RGBACOLOR(0, 0, 0, 0.7f)];
    //
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showRepairRemarksAtIndex:) name:@"showRepairRemarks" object:nil];
}

- (void)loadData
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getPayServerAddr], repairDetailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@&type=0", repairID];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            _evaluateContent = (ISNULL(responseDic[@"confirmContent"])) ? @"" : responseDic[@"confirmContent"];
            NSString *scoreValue = (ISNULL(responseDic[@"confirmLevel"])) ? @0 : responseDic[@"confirmLevel"];
            _evaluateScore = [scoreValue integerValue];
            //
            CGSize screenSize = [UIScreen mainScreen].bounds.size;
            CGFloat height = screenSize.height-64-53-45;
            CGRect frame = CGRectMake(0, 0, screenSize.width, height);
            //
            _statusView = [[BIDPersonalRepairStatusView alloc] initWithFrame:frame andDic:responseDic];
            [_containerView addSubview:_statusView];
            //
            _detailView = [[[NSBundle mainBundle] loadNibNamed:@"BIDSubPersonalRepairDetailView" owner:self options:nil] lastObject];
            _detailView.frame = frame;
            _detailView.delegate = self;
            [_detailView prepareViewWithDic:responseDic];
            [_containerView addSubview:_detailView];
            [_containerView bringSubviewToFront:_statusView];
            //
            NSNumber *statusValue = responseDic[@"status"];
            _curRepairStatus = [statusValue integerValue];
            [self setFunctionBtnTitleByStatus:[statusValue integerValue]];
        }
    }];
}

/**
 显示报修不同状态下的备注信息

 @param notification <#notification description#>
 */
- (void)showRepairRemarksAtIndex:(NSNotification *)notification
{
    NSDictionary *dic = notification.userInfo;
    NSIndexPath *indexPath = dic[@"indexPath"];
    NSUInteger row = indexPath.row;
    if(row <= _curRepairStatus)
    {
        if(row == 1)
        {
            //显示派工信息
            [self showDispatchInfo];
        }
        else if(row == 3)
        {
            //显示评价信息
            [self showEvaluateInfo];
        }
    }
    else
    {
        NSString *strMsg = @"";
        if(row == 1)
        {
            strMsg = @"还未派工";
        }
        else
        {
            strMsg = @"还未评价";
        }
        [self showAlertView:strMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
    }
}

/**
 显示派工信息
 */
- (void)showDispatchInfo
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getPayServerAddr], fetchDispatchInfoURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@", repairID];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            BIDDispatchInfoView *infoView = [[[NSBundle mainBundle] loadNibNamed:@"BIDDispatchInfoView" owner:self options:nil] lastObject];
            [infoView setBackgroundColor:[UIColor clearColor]];
            infoView.delegate = self;
            infoView.nameLabel.text = responseDic[@"repairUserName"];
            infoView.phoneLabel.text = responseDic[@"repairUserTel"];
            CGSize screenSize = [UIScreen mainScreen].bounds.size;
            CGRect frame = infoView.frame;
            frame.origin.y = (screenSize.height-64-45-45-CGRectGetHeight(frame)) / 2;
            frame.origin.x = (screenSize.width-CGRectGetWidth(frame)) / 2;
            infoView.frame = frame;
            [self.view addSubview:_maskView];
            [self.view addSubview:infoView];
        }
    }];
}

/**
 显示评价信息
 */
- (void)showEvaluateInfo
{
    BIDEvaluateInfoView *infoView = [[[NSBundle mainBundle] loadNibNamed:@"BIDEvaluateInfoView" owner:self options:nil] lastObject];
    [infoView setBackgroundColor:[UIColor clearColor]];
    infoView.delegate = self;
    infoView.contentTextView.text = _evaluateContent;
    [infoView setScore:_evaluateScore];
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGRect frame = infoView.frame;
    frame.origin.y = (screenSize.height-64-45-45-CGRectGetHeight(frame)) / 2;
    frame.origin.x = (screenSize.width-CGRectGetWidth(frame)) / 2;
    infoView.frame = frame;
    [self.view addSubview:_maskView];
    [self.view addSubview:infoView];
}
#pragma mark - BIDDispatchInfoViewDelegate
- (void)removeView
{
    [_maskView removeFromSuperview];
}

#pragma mark - BIDSubPersonalRepairDetailViewDelegate
- (void)callWithPhone:(NSString *)phone
{
    NSString *str = [[NSString alloc] initWithFormat:@"tel://%@", phone];
    NSURL *url = [NSURL URLWithString:str];
    UIWebView *telWebView = [[UIWebView alloc] init];
    [telWebView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:telWebView];
}

- (void)showPhotoAtIndex:(NSInteger)index withImgArr:(NSArray *)imgArr
{
    BIDBrowserImageViewController *vc = [[BIDBrowserImageViewController alloc] initWithNibName:@"BIDBrowserImageViewController" bundle:nil];
    vc.imgArr = imgArr;
    vc.index = index;
    [self.navigationController pushViewController:vc animated:YES];
}

/**
 设置底部功能按钮标题和响应方法

 @param status <#status description#>
 */
- (void)setFunctionBtnTitleByStatus:(NSInteger)status
{
    switch(status)
    {
        case 0:
        {
            //待处理情况下可以关闭报修
            [_functionBtn setTitle:@"撤单" forState:UIControlStateNormal];
            [_functionBtn addTarget:self action:@selector(closeRepair) forControlEvents:UIControlEventTouchUpInside];
        }
            break;
        case 1:
        {
            //确认完成
            [_functionBtn setTitle:@"确认完成" forState:UIControlStateNormal];
            [_functionBtn addTarget:self action:@selector(confirmComplete) forControlEvents:UIControlEventTouchUpInside];
        }
            break;
        case 2:
        case 3:
        {
            //评价
            [_functionBtn setTitle:@"评价" forState:UIControlStateNormal];
            [_functionBtn addTarget:self action:@selector(toEvaluate) forControlEvents:UIControlEventTouchUpInside];
        }
            break;
        case 6:
        {
            //已撤单
            _functionBtn.enabled = NO;
            [_functionBtn setBackgroundColor:RGBACOLOR(130, 130, 130, 1.f)];
            [_functionBtn setTitle:@"已撤单" forState:UIControlStateNormal];
        }
            break;
    }
}

/**
 关闭报修(撤单）
 */
- (void)closeRepair
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getPayServerAddr], closeRepairURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@", self.repairID];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
             [self showAlertView:@"操作成功" buttonTitle:ALERT_BTN_TITLE tag:@100 functionName:@"alertViewHandler"];
        }
    }];
}

/**
 确认完成
 */
- (void)confirmComplete
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getPayServerAddr], confirmRepairOrEvaluateURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"id=%@&status=2", repairID];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            [self showAlertView:@"操作成功" buttonTitle:ALERT_BTN_TITLE tag:@100 functionName:@"alertViewHandler"];
        }
    }];
}

/**
 评价
 */
- (void)toEvaluate
{
    BIDEvaluateForPersonalRepairViewController *vc = [[BIDEvaluateForPersonalRepairViewController alloc] init];
    vc.repairID = self.repairID;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)alertViewHandler
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)repairStatusBtnHandler:(id)sender
{
    if(_isShowRepairStatus)
    {
    }
    else
    {
        _isShowRepairStatus = YES;
        [_repairStatusBtn setTitleColor:RGBACOLOR(33.f, 183.f, 149.f, 1.f) forState:UIControlStateNormal];
        [_repairDetailBtn setTitleColor:RGBACOLOR(130.f, 130.f, 130.f, 1.f) forState:UIControlStateNormal];
        [_containerView bringSubviewToFront:_statusView];
    }
}

- (IBAction)repairDetailBtnHandler:(id)sender
{
    if(_isShowRepairStatus)
    {
        _isShowRepairStatus = NO;
        [_containerView bringSubviewToFront:_detailView];
        [_repairDetailBtn setTitleColor:RGBACOLOR(33.f, 183.f, 149.f, 1.f) forState:UIControlStateNormal];
        [_repairStatusBtn setTitleColor:RGBACOLOR(130.f, 130.f, 130.f, 1.f) forState:UIControlStateNormal];
    }
    else
    {
    }
}

@end
